package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LocalDateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.FwzpMapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwamlT;
import cn.e4j.modular.crm.entity.MmrdT;
import cn.e4j.modular.crm.entity.MmrfT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务派工
 */
@Service
public class FwzpService{

    @Autowired
    FwzpMapper fwzpMapper;

    /**
     * 获取服务单列表
     * mmrd020 3.派工  4 报工
     *
     * @param inDto
     * @return
     */
    public List<Dto> getServiceOrderListData(Page<Object> page, Dto inDto){
        List<Dto> list = fwzpMapper.getServiceOrderListPage(page,inDto);
        list.forEach(dto->{
            Dto query = new Dto();
            query.put("MMRF001",dto.getString("MMRDDOCNO"));
            query.put("ENT", inDto.getString("ENT"));

            List<Dto> dtos = fwzpMapper.getMmrf008(query);
            dto.put("MMRF008", ToolUtil.isNotEmpty(dtos)?dtos.get(0).getString("MMRF008"):"");
        });
        return list;
    }


    /**
     * 服务派工提交
     * @param inDto
     */
    @Transactional
    public List<Dto> saveFwpg(SysUser userModel,Dto inDto){
        LocalDate now = LocalDate.now();
        JSONArray orderList = JSONArray.parseArray(inDto.getString("orderList"));
        String mmrfdocno = "WXPG" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        List dtoList= new ArrayList<>();
        String userId = inDto.getString("userId");
        for (int i=0;i<orderList.size();i++){
            JSONObject order = orderList.getJSONObject(i);
            MmrdT mmrdT = CommonUtils.toJavaClass(order, MmrdT.class);
            GwamlT gwamlT = CommonUtils.toJavaClass(order, GwamlT.class);
            MmrfT mmrf_tPO = new MmrfT();
            mmrf_tPO.setMMRFENT(userModel.getDBC());
            mmrf_tPO.setMMRFDOCNO(mmrfdocno);
            mmrf_tPO.setMMRFSEQ(BigDecimal.valueOf(i+1));
            mmrf_tPO.setMMRFDOCDATE(now);
            mmrf_tPO.setMMRF001(mmrdT.getMMRDDOCNO());
            mmrf_tPO.setMMRF002(mmrdT.getMMRDSEQ());
            mmrf_tPO.setMMRF003(mmrdT.getMMRD002());
            mmrf_tPO.setMMRF004(mmrdT.getMMRD003());
            mmrf_tPO.setMMRF005(inDto.getString("MMRE004"));
            mmrf_tPO.setMMRF006(inDto.getString("orgId"));
            mmrf_tPO.setMMRF007(inDto.getString("userId"));

            //获取上门服务日期
            mmrf_tPO.setMMRF008(inDto.getDate("date").toLocalDate());
            mmrf_tPO.setMMRF013("");
            mmrf_tPO.setMMRFACTI("Y");
            mmrf_tPO.setMMRFUSER(userModel.getID());
            mmrf_tPO.setMMRFGRUP(userModel.getORG_ID());
            mmrf_tPO.setMMRFORIU(userModel.getID());
            mmrf_tPO.setMMRFORIG(userModel.getORG_ID());
            mmrf_tPO.setMMRFDATE(LocalDate.now());
            mmrf_tPO.setMMRFTIME(DateUtils.formatDate(new Date(),DateUtils.PATTERN_HH_mm_ss));

            if (mmrdT.getMMRD020()!=null){
                //将原有服务单号设为无效
                Dto updateDto = new Dto();
                updateDto.put("MMRDDOCNO",mmrdT.getMMRDDOCNO());
                fwzpMapper.updateMmrfTX(updateDto);
            }

            mmrf_tPO.insert();
            mmrdT.setMMRD020("3");
            mmrdT.setMMRDMODU(userModel.getID());
            //WHERE  mmrdent = #{mmrdent} AND mmrddocno = #{mmrddocno} AND mmrdseq = #{mmrdseq}
            UpdateWrapper<MmrdT> wrapper = new UpdateWrapper<>();
            wrapper.eq("MMRDENT",mmrdT.getMMRDENT());
            wrapper.eq("MMRDDOCNO",mmrdT.getMMRDDOCNO());
            wrapper.eq("MMRDSEQ",mmrdT.getMMRDSEQ());
            mmrdT.update(wrapper);

            //预约者
            String mmrduser = mmrdT.getMMRD017();
            String gwaml003 = gwamlT.getGWAML003();

            Dto mmraDto = new Dto();
            mmraDto.put("MMRD002", mmrdT.getMMRD002());
            mmraDto.put("MMRD003", mmrdT.getMMRD003());
            Dto mmra = fwzpMapper.getGwakl004(mmraDto);
            Dto msgDto = new Dto();
            msgDto.put("pguser",userId);
            msgDto.put("yyuser",mmrduser);
            msgDto.put("GWAML003",gwaml003);
            msgDto.put("mmrf008",mmrf_tPO.getMMRF008());
            msgDto.put("gwakl004", mmra.getString("GWAKL004"));

            dtoList.add(msgDto);
        }

        return dtoList;
    }
}
